package com.shenmazong.servereduresume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shenmazong.servereduresume.dto.TbCertificate;
import com.shenmazong.servereduresume.mapper.TbCertificateMapper;
import com.shenmazong.servereduresume.service.TbCertificateService;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.BatchChooseIdVo;
import com.shenmazong.vo.ListParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *
 */
@Service
public class TbCertificateServiceImpl extends ServiceImpl<TbCertificateMapper, TbCertificate>
implements TbCertificateService {

    @Autowired
    private TbCertificateMapper tbCertificateMapper;

    @Override
    public ResultResponse findAllCertificate(ListParamVo listParamVo) {
        Page<TbCertificate> page = tbCertificateMapper.selectPage(new Page<>(listParamVo.getCurrent(), listParamVo.getSize()
        ), new QueryWrapper<TbCertificate>().lambda().eq(TbCertificate::getDeleteFlag, 0));
        return ResultResponse.SUCCESS(page);
    }

    @Override
    public ResultResponse saveCertificate(TbCertificate tbCertificate) {
        try {
            tbCertificateMapper.insert(tbCertificate);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse updateCertificate(TbCertificate tbCertificate) {
        try {
            tbCertificate.setUpdateTime(new Date());
            tbCertificateMapper.updateById(tbCertificate);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse deleteCertificate(BatchChooseIdVo batchChooseIdVo) {
        if (batchChooseIdVo.getIds().size()==0){
            return ResultResponse.FAILED("至少选择一个删除");
        }
        try {
            for (Long id : batchChooseIdVo.getIds()) {
                TbCertificate certificate = tbCertificateMapper.selectById(id);
                certificate.setDeleteFlag((byte) 1);
                tbCertificateMapper.updateById(certificate);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.FAILED();
        }
        return ResultResponse.SUCCESS();
    }
}




